package com.soujiayi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.soujiayi.net.HttpRequester;
import com.soujiayi.net.NetworkUtils;
import com.soujiayi.util.Constants;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class CityListActivity extends Activity {
private ListView listView;
private LinearLayout ll_city_loading;
private SimpleAdapter simpleAdapter;
private List<Map<String,String>> cityList;
private HttpRequester httpRequester;
private Handler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.city_list);
		initViews();
		initValues();
	}

	public void initViews(){
		listView = (ListView)findViewById(R.id.lv_city);
		ll_city_loading = (LinearLayout)findViewById(R.id.ll_city_loading);
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				// TODO Auto-generated method stub
				String cityId = cityList.get(position).get("id");
				String title = cityList.get(position).get("title");
				Intent intent = CityListActivity.this.getIntent();
				intent.putExtra("id", cityId);
				intent.putExtra("title", title);
				CityListActivity.this.setResult(Activity.RESULT_OK,intent);
				CityListActivity.this.finish();
				overridePendingTransition(R.anim.push_down_in,
						R.anim.push_down_out);
			}
			
		});
	}
	
	public void initValues(){
		
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
			
				if(msg.what ==1)
				{
					ll_city_loading.setVisibility(View.GONE);
					List<Map<String,String>>  list = (List<Map<String,String>>)msg.obj;
					if(list!=null && list.size()>=0){
						cityList = new ArrayList<Map<String,String>>();
						cityList = list;
						simpleAdapter = new SimpleAdapter(CityListActivity.this, cityList,R.layout.city_list_item, new String[]{"id","title"}, new int[]{R.id.tv_city_id,R.id.tv_city_name});
						listView.setAdapter(simpleAdapter);
					}else
						Toast.makeText(CityListActivity.this, "城市列表获取失败!", Toast.LENGTH_SHORT).show();
				}
			}
			
		};
		httpRequester = new HttpRequester(this);
		if(!NetworkUtils.isNetworkConnected(this)){
			Toast.makeText(CityListActivity.this, this.getString(R.string.network_unavailable_message), Toast.LENGTH_SHORT).show();
			return;
		}
		ll_city_loading.setVisibility(View.VISIBLE);		
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Map<String,String> parameters = new HashMap<String,String>();
				parameters.put("sid", ((MyApplication)CityListActivity.this.getApplication()).getSessionId());
				List<Map<String,String>> list = httpRequester.getMemberCityList(parameters);
				Message msg = new Message();
				msg.what = 1;
				msg.obj  = list;
				handler.sendMessage(msg);
			}
			
		}).start();
	}
	public void back(View view) {
		this.finish();
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}
}
